/**
 * @author 黄波
 * 创建日期：2011Oct 10, 2011
 * TODO// N/A
 */
package com.platform.njcc.action;

import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.platform.os.util.Mail;
import com.platform.pojo.UserInfo;
import com.platform.user.bo.UserBo;

/**
 * 用户注册
 */
public class RegisterAction extends NJCCSuport {

	private UserInfo user;
	private String verifyCode;
	
	@Override
	public String execute() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		//验证参数
		if(user == null) {
			out.flush();
			out.write("<script>alert('用户不可为空！');history.back();</script>");
			out.close();
			return null;
		}
		if(user.getLoginName() == null || user.getLoginName().trim().equals("")) {
			out.flush();
			out.write("<script>alert('用户名不可为空！');history.back();</script>");
			out.close();
			return null;
		}
		if(user.getLoginPasswd() == null || user.getLoginPasswd().trim().equals("")) {
			out.flush();
			out.write("<script>alert('密码不可为空！');history.back();</script>");
			out.close();
			return null;
		}
		if(user.getRealName() == null || user.getRealName().trim().equals("")) {
			out.flush();
			out.write("<script>alert('真实姓名不可为空！');history.back();</script>");
			out.close();
			return null;
		}
		if(user.getEmail() == null || user.getEmail().trim().equals("")) {
			out.flush();
			out.write("<script>alert('邮箱地址不可为空！');history.back();</script>");
			out.close();
			return null;
		}
		HttpSession session = ServletActionContext.getRequest().getSession();
		//验证验证码是否填写正确
		if(!session.getAttribute("rand").toString().toLowerCase().equals(verifyCode.trim().toLowerCase())) {
			out.flush();
			out.write("<script>alert('验证码填写错误！');history.back();</script>");
			out.close();
			return null;
		}
		//验证当前数据库中是否已有该用户
		UserInfo dbUser = UserBo.getInstance().getUser1(user.getLoginName());
		if(dbUser != null) {
			out.flush();
			out.write("<script>alert('用户名已被占用！');history.back();</script>");
			out.close();
			return null;
		}
		//验证邮箱是否已被注册
		dbUser = UserBo.getInstance().getUserByEamil(user.getEmail());
		if(dbUser != null){
			out.flush();
			out.write("<script>alert('邮箱已被注册！');history.back();</script>");
			out.close();
			return null;
		}
		//否则进行注册
		user.setRegTime(new Date());
		user.setGroupId(0);
		user.setIsAudit(0);
		user.setScore(100);
		user.setShareCount(0);
		user.setDownloadCount(0);
		user.setDownloadedCount(0);
		user.setUserScore(0);
		UserBo.getInstance().addUser(user);
		session.setAttribute("user", user);
		//发送邮件操作
//		new Thread() {
//			public void run() {
//				Mail.sendMail("注册成功-南京化工职业技术学院资源共享平台！", "<br/>您的密码为：" + user.getLoginPasswd() + "，您的帐户将在通过审核后才能成功上传、下载资源。<a href='' style='color:red;'>点击这里，进行访问</a>！<br/><br/>---------------<br/>南京化工职业技术学院--资源共享平台<br/>", user.getEmail());
//			}
//		}.start();
		//更新用户登录IP和时间
		user.setLastLoginIp(ServletActionContext.getRequest().getRemoteAddr());
		user.setLastLoginTime(new Date());
		UserBo.getInstance().updateUser(user);
		out.flush();
		out.write("<script>alert('注册成功！');window.location.href='../entry.jsp';</script>");
		out.close();
		return null;
	}

	public String getVerifyCode() {
		return verifyCode;
	}

	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}

	public UserInfo getUser() {
		return user;
	}

	public void setUser(UserInfo user) {
		this.user = user;
	}
	
}
